﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace RenderHeads.Media.AVProVideo.Demos
{
	// Token: 0x0200000B RID: 11
	public class SampleApp_Multiple : MonoBehaviour
	{
		// Token: 0x06000034 RID: 52 RVA: 0x000036D0 File Offset: 0x000018D0
		private void Update()
		{
			foreach (DisplayUGUI displayUGUI in this.m_aAddedVideos)
			{
				if (displayUGUI.gameObject != null && !displayUGUI.gameObject.activeSelf && displayUGUI._mediaPlayer != null && displayUGUI._mediaPlayer.Control != null && displayUGUI._mediaPlayer.Control.IsPlaying())
				{
					displayUGUI.gameObject.SetActive(true);
				}
			}
		}

		// Token: 0x06000035 RID: 53 RVA: 0x00003788 File Offset: 0x00001988
		private void UpdateVideosLayout()
		{
			GameObject gameObject = GameObject.Find("Canvas/Panel");
			RectTransform rectTransform = (!gameObject) ? null : gameObject.GetComponent<RectTransform>();
			if (rectTransform)
			{
				Vector2 sizeDelta = rectTransform.sizeDelta;
				Vector2 vector = new Vector2(sizeDelta.x * 0.5f, sizeDelta.y * 0.5f);
				int count = this.m_aAddedVideos.Count;
				int num = Mathf.CeilToInt(Mathf.Sqrt((float)count));
				float num2 = 1f / (float)num * sizeDelta.x;
				float num3 = 1f / (float)num * sizeDelta.y;
				for (int i = 0; i < count; i++)
				{
					DisplayUGUI displayUGUI = this.m_aAddedVideos[i];
					int num4 = i % num;
					int num5 = i / num;
					displayUGUI.rectTransform.anchoredPosition = new Vector2(num2 * (float)num4 - vector.x, num3 * (float)(-(float)num5) + vector.y);
					displayUGUI.rectTransform.sizeDelta = new Vector2(num2, num3);
				}
			}
		}

		// Token: 0x06000036 RID: 54 RVA: 0x000038A4 File Offset: 0x00001AA4
		public void AddVideoClicked()
		{
			this.m_NumVideosAdded++;
			GameObject gameObject = new GameObject("AVPro MediaPlayer " + this.m_NumVideosAdded.ToString());
			MediaPlayer mediaPlayer = gameObject.AddComponent<MediaPlayer>();
			mediaPlayer.m_VideoPath = this.m_videoPath;
			mediaPlayer.m_AutoStart = true;
			mediaPlayer.m_Loop = true;
			mediaPlayer.SetGuiPositionFromVideoIndex(this.m_NumVideosAdded - 1);
			mediaPlayer.SetDebugGuiEnabled(this.m_NumVideosAdded < 5);
			GameObject gameObject2 = GameObject.Find("Canvas/Panel");
			if (gameObject2 != null)
			{
				GameObject gameObject3 = new GameObject("AVPro Video uGUI " + this.m_NumVideosAdded.ToString());
				gameObject3.transform.parent = gameObject2.transform;
				gameObject3.SetActive(false);
				gameObject3.AddComponent<RectTransform>();
				gameObject3.AddComponent<CanvasRenderer>();
				DisplayUGUI displayUGUI = gameObject3.AddComponent<DisplayUGUI>();
				displayUGUI._mediaPlayer = mediaPlayer;
				displayUGUI._scaleMode = ScaleMode.StretchToFill;
				displayUGUI.rectTransform.localScale = Vector3.one;
				displayUGUI.rectTransform.pivot = new Vector2(0f, 1f);
				this.m_aAddedVideos.Add(displayUGUI);
				this.UpdateVideosLayout();
			}
		}

		// Token: 0x06000037 RID: 55 RVA: 0x000039D4 File Offset: 0x00001BD4
		public void RemoveVideoClicked()
		{
			if (this.m_aAddedVideos.Count > 0)
			{
				int index = UnityEngine.Random.Range(0, this.m_aAddedVideos.Count);
				DisplayUGUI displayUGUI = this.m_aAddedVideos[index];
				if (displayUGUI._mediaPlayer != null)
				{
					displayUGUI._mediaPlayer.CloseVideo();
					UnityEngine.Object.Destroy(displayUGUI._mediaPlayer.gameObject);
					displayUGUI._mediaPlayer = null;
				}
				UnityEngine.Object.Destroy(displayUGUI.gameObject);
				this.m_aAddedVideos.RemoveAt(index);
				this.m_NumVideosAdded--;
			}
		}

		// Token: 0x06000038 RID: 56 RVA: 0x00003A6C File Offset: 0x00001C6C
		private void OnDestroy()
		{
			foreach (DisplayUGUI displayUGUI in this.m_aAddedVideos)
			{
				if (displayUGUI._mediaPlayer)
				{
					displayUGUI._mediaPlayer = null;
				}
			}
			this.m_aAddedVideos.Clear();
		}

		// Token: 0x0400002C RID: 44
		public string m_videoPath = "BigBuckBunny_360p30.mp4";

		// Token: 0x0400002D RID: 45
		private int m_NumVideosAdded;

		// Token: 0x0400002E RID: 46
		private List<DisplayUGUI> m_aAddedVideos = new List<DisplayUGUI>();
	}
}
